package com.kgcx.dbutils.config.factory;

import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import org.springframework.core.io.support.ResourcePropertySource;
import org.springframework.lang.Nullable;

import java.io.IOException;

/**
 * @author 吴刚林
 * @PropertySource注解默认只能支持properties文件,采用此factory可支持yml
 * @date 2017/12/4
 */
public class YamlPropertySourceFactory implements PropertySourceFactory {


    @Override
    public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {
        YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();
        return (name != null ? yamlPropertySourceLoader.load(name, resource.getResource()).get(0) : new ResourcePropertySource(name, resource));
    }

}
